272C - Dima and Staircase - CodeForces Solution


data structures implementation *1500

Please click on ads to support us..

C++ Code:

#include<bits/stdc++.h>
using namespace std;
#define int long long
int tree[100005*4];
int lazy[100005*4];
int s[100005*4];
int n,m;
void build(int node,int l,int r){
	if(l==r){
        tree[node]=0;
		return ;
	}
	int mid=(l+r)>>1;
	build(node*2,l,mid);
	build(node*2+1,mid+1,r);
	tree[node]=tree[node*2]+tree[node*2+1];
}
void pushdown(int node,int x,int y){
	if(lazy[node]){
		lazy[node<<1]+=lazy[node];
		lazy[node<<1|1]+=lazy[node];
		tree[node<<1]+=lazy[node]*x;
		tree[node<<1|1]+=lazy[node]*y;
		lazy[node]=0;
	}
}
int query(int node,int l,int r,int L,int R){
    if(l>=L&&r<=R){
        return tree[node];
    }
    int mid=(l+r)>>1;
    pushdown(node,mid-l+1,r-mid);
    int ans=0;
    if(L<=mid){
        ans+=query(node<<1,l,mid,L,R);
    }
    if(R>mid){
        ans+=query(node<<1|1,mid+1,r,L,R);
    }
    return ans;
}
void update(int node,int l,int r,int L,int R,int val){
	if(l>=L&&r<=R){
		tree[node]=(r-l+1)*val;
		lazy[node]+=val;
		return ;
	}
	int mid=l+r>>1;
	pushdown(node,mid-l+1,r-mid);
	if(L<=mid){
		update(node<<1,l,mid,L,R,val);
	}
	if(R>mid){
		update(node<<1|1,mid+1,r,L,R,val);
	}
    tree[node]=tree[node*2]+tree[node*2+1];
}
signed main(){

	while(cin>>n){
       for(int i =1;i<=n;i++){
           cin>>s[i];
       }
       cin>>m;
        int hh=0,h1=0,w1;
       while(m--){
           int l,r;
           cin>>l>>r;
            hh=max(hh+h1,s[l]);
            w1=l;
            h1=r;
            cout<<hh<<endl;
       }
	}



	return 0;
}


Comments

Submit
0 Comments
More Questions

84. Largest Rectangle in Histogram
60. Permutation Sequence
42. Trapping Rain Water
32. Longest Valid Parentheses
Cutting a material
Bubble Sort
Number of triangles
AND path in a binary tree
Factorial equations
Removal of vertices
Happy segments
Cyclic shifts
Zoos
Build a graph
Almost correct bracket sequence
Count of integers
Differences of the permutations
Doctor's Secret
Back to School
I am Easy
Teddy and Tweety
Partitioning binary strings
Special sets
Smallest chosen word
Going to office
Color the boxes
Missing numbers
Maximum sum
13 Reasons Why
Friend's Relationship